// -*- C++ -*-

// Copyright 2006-2007 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
// or its licensors, as applicable.
//
// You may not use this file except under the terms of the accompanying license.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the License. You may
// obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Project:
// File:
// Purpose:
// Responsible: kapry
// Reviewer:
// Primary Repository:
// Web Sites: www.iupr.org, www.dfki.de

#include "colib.h"
#include "charlib.h"
#include "report.h"

using namespace colib;

namespace ocropus {

    struct SliceLibrary : public ICharacterLibrary {
        ICharacterLibrary &charlib;
        int from, to;

        virtual const char *description() {
            return "charlib slice";
        }

        virtual void init(const char **argv) {
        }

        virtual int sectionsCount() {
            return to - from;
        }

        virtual void switchToSection(int no) {
            charlib.switchToSection(from + no);
        }

        virtual int currentSectionIndex() {
            return charlib.currentSectionIndex() - from;
        }

        virtual int charactersCount() {
            return charlib.charactersCount();
        }

        virtual ICharacter &character(int index) {
            return charlib.character(index);
        }

        SliceLibrary(ICharacterLibrary &l, int f, int t) : charlib(l), from(f), to(t) {
            ASSERT(0 <= f && f < l.sectionsCount());
            ASSERT(0 <= t && t <= l.sectionsCount());
            ASSERT(f <= t);
        }
    };

    ICharacterLibrary *make_slice_charlib(ICharacterLibrary &charlib, int from_incl, int upto_excl) {
        return new SliceLibrary(charlib, from_incl, upto_excl);
    }


    void dump_charlib(const char *path, ICharacterLibrary &charlib) {
        autodel<IReport> report(make_HTML_report(path));
        for(int i = 0; i < charlib.sectionsCount(); i++) {
            charlib.switchToSection(i);
            report->add("<H3>Section %d</H3>", i);
            for(int j = 0; j < charlib.charactersCount(); j++) {
                report->add("%s", charlib.character(j).text());
                report->add(charlib.character(j).image());
            }
        }
    }

}; //namespace
